package com.yeyks.commonReference.service.vip.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.commonReference.service.vip.AccountVipSalerRelationService;
import com.yeyks.vip.dal.domain.AccountVipSalerRelation;
import com.yeyks.vip.dal.dao.AccountVipSalerRelationMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 会员卡运营商服务商关系表 服务实现类
 * </p>
 *
 * @author yun
 * @since 2019-08-23
 */
@Service
public class AccountVipSalerRelationServiceImpl extends ServiceImpl<AccountVipSalerRelationMapper, AccountVipSalerRelation> implements AccountVipSalerRelationService {

    /**
     * 通过accountId查询会员卡运营商服务商关系对象
     *
     * @param accountId
     * @param vipType   vip类型，1大V，2小V
     * @return
     */
    @Override
    public AccountVipSalerRelation selectByAccountId(Integer accountId, Integer vipType) {
        return baseMapper.selectOne(new QueryWrapper<AccountVipSalerRelation>()
                .eq(AccountVipSalerRelation.ACCOUNT_ID, accountId)
                .eq(AccountVipSalerRelation.VIP_TYPE, vipType)
                .last("limit 1"));
    }

    /**
     * 通过accountId和vipType和上级账户类型查询上级
     *
     * @param accountId
     * @param vipType    vip类型，1大V，2小V
     * @param parentType {@link com.yeyks.common.constants.AccountType}
     * @return
     */
    @Override
    public AccountVipSalerRelation selectByAccountId(Integer accountId, Integer vipType, Integer parentType) {
        return baseMapper.selectByAccountId(accountId, vipType, parentType);
    }

    /**
     * 通过parentId查询子对象
     *
     * @param parentId
     * @param vipType  vip类型，1大V，2小V
     * @return
     */
    @Override
    public List<AccountVipSalerRelation> selectByParentId(Integer parentId, Integer vipType) {
        return baseMapper.selectList(new QueryWrapper<AccountVipSalerRelation>()
                .eq(AccountVipSalerRelation.PARENT_ID, parentId).eq(AccountVipSalerRelation.VIP_TYPE, vipType));
    }

    /**
     * 通过parentId查询子对象
     *
     * @param parentId
     * @param vipType  vip类型，1大V，2小V
     * @return
     */
    @Override
    public List<Integer> selectAccountIdsByParentId(Integer parentId, Integer vipType) {
       return Lists.transform(selectByParentId(parentId, vipType), AccountVipSalerRelation::getAccountId);

    }

    @Override
    public List<Integer> listByChildrenAndMySelf(AccountInfo loginUser) {
        List<Integer> salerIdList = listByChildren(loginUser);
        salerIdList.add(loginUser.getId());
        return salerIdList;
    }

    @Override
    public List<Integer> listByChildren(AccountInfo loginUser) {
        // 查询子集，(3.8版本中服务商去除， 不影响逻辑。只是少查询一层) 运营商 --> 服务商 --> 卡商
        List<Integer> salerIdList = Lists.newArrayList();
        List<Integer> firstAccountIds = Lists.newArrayList();
        List<Integer> secondAccountIds = Lists.newArrayList();
        //查询出下级的卡片绑定情况
        List<AccountVipSalerRelation> vipSalerRelationList = this.baseMapper.selectList(new QueryWrapper<AccountVipSalerRelation>()
                .eq(AccountVipSalerRelation.PARENT_ID, loginUser.getId()));
        if (!CollUtil.isEmpty(vipSalerRelationList)) {
            firstAccountIds = vipSalerRelationList.stream().map(AccountVipSalerRelation::getAccountId).collect(Collectors.toList());
            salerIdList.addAll(firstAccountIds);
            List<AccountVipSalerRelation> childrenRelationList = this.baseMapper.selectList(new QueryWrapper<AccountVipSalerRelation>()
                    .in(AccountVipSalerRelation.PARENT_ID, firstAccountIds));
            if (!CollUtil.isEmpty(childrenRelationList)) {
                secondAccountIds = childrenRelationList.stream().map(AccountVipSalerRelation::getAccountId).collect(Collectors.toList());
                salerIdList.addAll(secondAccountIds);
            }
        }
        return salerIdList;
    }
}
